﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeChatSDK.Core.Models;

namespace WeChatSDK.Core
{
    public class UserManage
    {

        public string GetUserBaseInfo(string code)
        {
            CommunicateHelper conn = CommunicateHelper.init();
            string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", conn.AppID, conn.AppSecret, code);
            string rec = conn.HttpGet(url);

            //"access_token":"ACCESS_TOKEN",
            //"expires_in":7200,
            //"refresh_token":"REFRESH_TOKEN",
            //"openid":"OPENID",
            //"scope":"SCOPE",
            //"unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"

            return string.Empty;
        }

        public UserInfoModel GetUserInfo(string openid)
        {
            CommunicateHelper conn = CommunicateHelper.init();
            string url = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", conn.Token, openid);
            string rec = conn.HttpGet(url);
            //"openid":" OPENID",
            //  " nickname": NICKNAME,
            //  "sex":"1",
            //  "province":"PROVINCE"
            //  "city":"CITY",
            //  "country":"COUNTRY",
            //   "headimgurl":    "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46", 
            //"privilege":[
            //"PRIVILEGE1"
            //"PRIVILEGE2"
            //   ],
            //   "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
            UserInfoModel model = null;
            if (rec.Length > 0)
            {
                model = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInfoModel>(rec);
            }

            return model;
        }

        public List<UserInfoModel> GetUserList()
        {
            return null;
        }
    }
}
